package at.fhooe.mc.duel_game.activities;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import at.fhooe.mc.duel_game.GameLogic;
import at.fhooe.mc.duel_game.GuiHelpers;
import at.fhooe.mc.duel_game.R;
import at.fhooe.mc.duel_game.bluetooth.BluetoothConnectionService;

public class GameMenuActivity extends Activity implements OnClickListener {

	private BluetoothAdapter mBluetoothAdapter;

	private BluetoothConnectionService mBluetoothConnectionService;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_game_menu);

		Button b = (Button) findViewById(R.id.ButtonStartTraining);
		b.setOnClickListener(this);

		b = (Button) findViewById(R.id.buttonStartServer);
		b.setOnClickListener(this);

		b = (Button) findViewById(R.id.buttonJoinServer);
		b.setOnClickListener(this);

		GameLogic.getInstance().setGameMenuActivity(this);

		// Get local Bluetooth adapter
		mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

		// If the adapter is null, then Bluetooth is not supported
		if (mBluetoothAdapter == null) {
			Toast.makeText(this, "Bluetooth is not available",
					Toast.LENGTH_LONG).show();
			finish();
			return;
		}
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();

		if (mBluetoothConnectionService == null)
			setupService();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		if (mBluetoothAdapter.isEnabled()
				&& mBluetoothConnectionService != null) {
			// Only if the state is STATE_NONE, do we know that we haven't
			// started already
			if (mBluetoothConnectionService.getState() == BluetoothConnectionService.STATE_NONE) {
				// Start the Bluetooth connection services
				mBluetoothConnectionService.start();
			}
		}
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();

		// if (mBluetoothConnectionService != null)
		// mBluetoothConnectionService.stop();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);

		// back button pressed
		if (data == null)
			return;

		Bundle bundle = data.getExtras();

		switch (requestCode) {
		case GetDevicesActivity.REQUEST_CONNECT_DEVICE:
			if (bundle != null) {

				GuiHelpers.showProgressDlg(this,
						getResources()
								.getString(R.string.dialogJoiningOpponent));

				String deviceAddress = bundle
						.getString(GetDevicesActivity.EXTRA_DEVICE_ADDRESS);

				if (deviceAddress != null) {

					// Get the BLuetoothDevice object
					BluetoothDevice device = mBluetoothAdapter
							.getRemoteDevice(deviceAddress);
					// Attempt to connect to the device
					mBluetoothConnectionService.connect(device);
				}
			}
			break;
		}
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.buttonJoinServer:
			GameLogic.getInstance().connectToServer();

			Intent intent = new Intent(getApplicationContext(),
					GetDevicesActivity.class);
			startActivityForResult(intent, 1);
			break;

		case R.id.buttonStartServer:
			GuiHelpers.showProgressDlg(this,
					getResources().getString(R.string.dialogWaitForOpponent));
			GameLogic.getInstance().startServer();
			
			// restart threads to accept net connections
			if(mBluetoothConnectionService != null) {
				mBluetoothConnectionService.start();
			}
			break;

		case R.id.ButtonStartTraining:
			Intent intent2 = new Intent(getApplicationContext(),
					GameActivity.class);
			intent2.putExtra("TRAINING", true);
			startActivity(intent2);
			break;
		}
	}

	private void setupService() {
		GameLogic.getInstance().initBluetoothConnectionService(
				getApplicationContext());

		mBluetoothConnectionService = GameLogic.getInstance()
				.getBluetoothService();
	}
}